package com.hcb.tonguetiphealth.widget;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.hcb.tonguetiphealth.adapter.CommentListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.model.Comment;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.util.StringUtils;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.drinksbook.DrinksBookDrinkInfoWidget;
import com.hcb.tonguetiphealth.widget.resbook.RestaurantBookInfoWidget;

public class CommentWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "CommentWidget";
	private String url = Global.URL + Global.servlet_commentInfo;
	
	private Activity ctx;
	private Handler mHandler;
	private LinearLayout back, add;
	private ListView commentListView;
	private List<Comment> commentList;
	private Comment comment;
	private CommentListAdapter commentAdapter;
	
	private String typeId;
	private String targetId;
	private boolean formCommentAdd;
	
	public CommentWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.comment, this); 
		
		/*
		 *  typeId = 17   // 酒水
			typeId = 4   // 商家评论
			typeId = 16   // 套餐
			typeId = 16   // 菜品
		 */
		typeId = ctx.getIntent().getStringExtra("typeId");
		targetId = ctx.getIntent().getStringExtra("targetId");
		formCommentAdd = ctx.getIntent().getBooleanExtra("commentSucceed", false);
		
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在获取评论列表...");
					break;
				case MessageWhat.DIMISS_DIALOG:
					commentAdapter = new CommentListAdapter(ctx, commentList);
					commentListView.setAdapter(commentAdapter);
					
					dismissMessageDialog();
					break;
				}
			}
		};
		back = (LinearLayout)findViewById(R.id.comment_back_button);
		add = (LinearLayout)findViewById(R.id.comment_add_button);
		back.setOnClickListener(this);
		add.setOnClickListener(this);
		
		commentListView = (ListView)findViewById(R.id.comment_listview);
		commentList = new ArrayList<Comment>();
		
		initData();
	}
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.comment_back_button:
			if("17".equals(typeId) ){
				ctx.setContentView(new DrinksBookDrinkInfoWidget(ctx));
			}else if("4".equals(typeId)){
				ctx.setContentView(new RestaurantBookInfoWidget(ctx));
			}
			break;
		case R.id.comment_add_button:
			ctx.setContentView(new CommentAddWidget(ctx));
			break;
		default:
			break;
		}
	}
	
	private void initData() {
		if(formCommentAdd) {
			commentList = Global.commentList;
			mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
			return;
		}
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);

		new Thread() {
			public void run() {
				String response = null;
				try {
					typeId = typeId==null ? "" : typeId;
					targetId = targetId==null ? "" : targetId;
					List <NameValuePair> params = new ArrayList <NameValuePair>();
					params.add(new BasicNameValuePair("typeId", typeId));
					params.add(new BasicNameValuePair("targetId", targetId));//373
					response = HttpUtils.Post(url, params);
					if(Global.DEBUG) {
						System.out.println("-----------------------------\n" + response);
					}
					
				} catch(Exception e) {
					Log.e(TAG, "Exception", e);
				}
				if(!StringUtils.isEmpty(response)) {
					JSONManager jManager = new JSONManager();
					boolean isGotJSONObject = jManager.isGotJSONObject(response);
					if(isGotJSONObject) {
						JSONArray jList = jManager.getJsonList();
						if(null == jList) {
							
						} else {
							for(int i=0; i< jList.length(); i++) {
								JSONObject jObject = getJSONObject(jList, i);
								if(null != jObject) {
									comment = new Comment();
									
									comment.setUser(getString(jObject, "username"));
									comment.setTime(getString(jObject, "addtime"));
									comment.setContent(getString(jObject, "content"));
									comment.setProduct(getString(jObject, "produce"));
									comment.setEnvironment(getString(jObject, "environment"));
									comment.setServe(getString(jObject, "serve"));
									comment.setRating(StringUtils.parseFloat(getString(jObject, "star")));
									
									commentList.add(comment);
								}
							}
						}
						int count = jManager.getResult();
						String msg = jManager.getMsg();
						System.out.println("------ 得到msg: " + count + ":" + msg);
					}
				}
				mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
			}
		}.start();
	}
	@Override
	public void notifyAdapter() {
		
	}
}
